package com.wtx.chapter07;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 * @description: leetcode 349  给定两个数组，编写一个函数来计算它们的交集。
 * @author: wtx
 * @createDate: 2020/5/27
 */
public class Solution349 {

    public int[] intersection(int[] nums1, int[] nums2) {

        TreeSet<Integer> set = new TreeSet<>();
        for (int num: nums1)
            set.add(num);

        ArrayList<Integer> list = new ArrayList<>();
        for (int num: nums2)
            if (set.contains(num)){
                list.add(num);
                set.remove(num);
            }

        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++)
            res[i] = list.get(i);
        return res;
    }
}
